/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include <cstdio>
#include <memory>
#if defined(_WINDOWS)
#if defined(_DEBUG)
#include <crtdbg.h>
#endif // _DEBUG
#include <WinSock2.h>
#endif // _WINDOWS
#include <UGame/System/Error.h>
#include <UGame/Util/Application.h>
using UGame::Util::Application;
using namespace UGame;

int main(int _Argc, char* _Argv[])
{
    int _Rc = Application::EXIT_OK;
    {
#if defined(_WINDOWS)
        /* Filter all unhandled exception like NULL-Pointer exception */
        ::SetUnhandledExceptionFilter(Application::UnhandledExceptionFilter);
        /* Initialize WIN32 Sockets */
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2, 2), &wsaData))
        {
            printf("WSAStartup() failed, %s",
                System::GetErrorString(System::GetLastError()).c_str());
            return -1;
        }
#endif // _WINDOWS

        /* Run application */
        {
            std::auto_ptr<Application> _App(Application::Create());
            if (_App.get() == NULL) {
                printf("Create Application failed, %s", 
                    System::GetErrorString(System::GetLastError()).c_str());
                return -1;
            }
            _Rc = _App->Run(_Argc, _Argv);
        }

#if defined(_WINDOWS)
        /* Uninitialize WIN32 Sockets */
        WSACleanup();
#endif // _WINDOWS

#if defined(_WINDOWS) && defined(_DEBUG)
        _CrtDumpMemoryLeaks();
#endif
    }

    return _Rc;
}
